package com.mer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mer.entity.Subject;
import com.mer.mapper.SubjectMapper;
import com.mer.service.SubjectService;
import com.mer.vo.CommonResult;
import com.mer.vo.SubjectVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: IIPhase-project
 * @description: 课程分类service接口实现类
 * @author: 陈铠
 * @create: 2021-10-19 10:46
 **/
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    @Resource
    private SubjectMapper subjectMapper;

    /**
     * 分页查询，条件搜索
     * @param page
     * @param limit
     * @param subjectVo
     * @return
     */
    @Override
    public CommonResult selectSubjectByPage(Integer page, Integer limit, SubjectVo subjectVo) {
        //查询框中有条件时,标题、状态
        if (StringUtils.isNotEmpty(subjectVo.getTitle()) || StringUtils.isNotEmpty(subjectVo.getStatusId())) {
            Page<Subject> page1 = new Page<>(page, limit);
            //条件
            QueryWrapper<Subject> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(subjectVo.getTitle())) {
                //标题不为空
                wrapper.like("title", subjectVo.getTitle());
            }
            if (StringUtils.isNotEmpty(subjectVo.getStatusId())) {
                //状态不为空
                wrapper.like("status_id", subjectVo.getStatusId());
            }
            //查找parent_id为0的父课程
            wrapper.eq("parent_id", "0");
            Page<Subject> subjectPage = subjectMapper.selectPage(page1, wrapper);
            List<Subject> subjectPageRecords = subjectPage.getRecords();

            //没有查询到一级分类，则查询子分类
            if (subjectPage.getTotal() == 0) {
                QueryWrapper<Subject> wrapper1 = new QueryWrapper<>();
                if (StringUtils.isNotEmpty(subjectVo.getTitle())) {
                    wrapper1.like("title", subjectVo.getTitle());
                }
                if (StringUtils.isNotEmpty(subjectVo.getStatusId())) {
                    wrapper1.like("status_id", subjectVo.getStatusId());
                }
                Page<Subject> page2 = subjectMapper.selectPage(page1, wrapper1);
                return new CommonResult(200, "查询成功", page2);
            } else {
                //查询到一级分类，继续递归
                List<Subject> subjects = subjectMapper.selectList(null);
                //为一级分类赋children值
                for (Subject subject : subjectPageRecords) {
                    //调用selectChildren方法将子课程装进父课程中
                    subject.setChildren(selectChildren(subjects, subject.getId()));
                }
                return new CommonResult(200, "查询成功", subjectPage);
            }
        } else {
            //加载页面时显示所有课程分类
            Page<Subject> subjectPage = new Page<>();
            QueryWrapper<Subject> subjectQueryWrapper = new QueryWrapper<>();
            subjectQueryWrapper.eq("parent_id", "0");
            Page<Subject> subjectPage1 = subjectMapper.selectPage(subjectPage, subjectQueryWrapper);
            List<Subject> records = subjectPage1.getRecords();
            //所有课程分类
            List<Subject> subjects = subjectMapper.selectList(null);
            //为父课程赋子课程
            for (Subject subject : records) {
                subject.setChildren(selectChildren(subjects, subject.getId()));
            }
            return new CommonResult(200, "查询成功", subjectPage1);
        }
    }

    /**
     * 分页查询调用的方法
     * 遍历子课程的方法
     * @param subjects
     * @param pid
     * @return
     */
    //查询所有子课程分类
    private List<Subject> selectChildren(List<Subject> subjects, String pid) {
        ArrayList<Subject> children = new ArrayList<>();
        //遍历父课程的子课程
        for (Subject subject : subjects) {
            //子课程的父id与父课程id相等的话，将该子课程添加到父课程下
            if (subject.getParentId().equals(pid)) {
                children.add(subject);
            }
        }
        //遍历子课程，装到children里
        for (Subject subjectChild : children) {
            subjectChild.setChildren(selectChildren(subjects, subjectChild.getId()));
        }
        return children;
    }

    /**
     * 增添父课程
     * 增添子课程的方法
     * @param subject
     * @return
     */
    @Override
    public CommonResult insertSubject(Subject subject) {
        int i = subjectMapper.insertSubject(subject);
        if (i > 0) {
            return new CommonResult(200, "添加成功", "yes");
        } else {
            return new CommonResult(500, "添加失败", "no");
        }
    }

    /**
     * 删除课程方法，
     * 若删除父课程，子课程一并删除
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteSubject(String id) {
        List<Subject> subjects = subjectMapper.selectList(null);
        int i = deleteSubjectMenu(id, subjects);
        if (i > 0) {
            return new CommonResult(200, "删除成功", "yes");
        } else {
            return new CommonResult(500, "删除失败", "yes");
        }
    }

    /**
     * 递归删除方法
     * @param id
     * @param subjects
     * @return
     */
    private int deleteSubjectMenu(String id, List<Subject> subjects) {
        for (Subject subject : subjects) {
            if (id.equals(subject.getParentId())) {
                deleteSubjectMenu(subject.getId(), subjects);
            }
        }
        int i = subjectMapper.deleteById(id);
        return i;
    }

    /**
     * 修改课程方法
     * @param subject
     * @return
     */
    @Override
    public CommonResult updateSubject(Subject subject) {
        int i = subjectMapper.updateById(subject);
        if (i > 0) {
            return new CommonResult(200, "修改成功", "yes");
        } else {
            return new CommonResult(500, "修改失败", "no");
        }
    }

    /**
     * 修改课程状态的方法
     * @param subjectVo
     * @return
     */
    @Override
    public CommonResult changeStatus(SubjectVo subjectVo) {
        int i = 0;
        if (subjectVo.getParentId().equals("0") && subjectVo.getStatusId().equals("0")) {
            subjectMapper.changeStatusById(subjectVo.getId(), subjectVo.getStatusId());
            QueryWrapper<Subject> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id", subjectVo.getId());
            List<Subject> subjects = subjectMapper.selectList(wrapper);
            /**
             * 递归修改
             */
            for (Subject subject : subjects) {
                subject.setStatusId("0");
                i = subjectMapper.changeStatusById(subject.getId(), subject.getStatusId());
            }
            if (i > 0) {
                return new CommonResult(200, "修改状态成功", "yes");
            } else {
                return new CommonResult((500), "修改状态失败", "yes");
            }
        } else {
            i = subjectMapper.changeStatusById(subjectVo.getId(), subjectVo.getStatusId());
            if (i > 0) {
                return new CommonResult(200, "修改状态成功", "yes");
            } else {
                return new CommonResult((500), "修改状态失败", "yes");
            }
        }
    }

    /**
     * 查询课程分类填充下拉框
     * @param page
     * @param limit
     * @return
     */
    @Override
    public CommonResult selectSubjectByStatus(Integer page, Integer limit) {
        Page<Subject> subjectPage = new Page<>(page, limit);
        QueryWrapper<Subject> subjectQueryWrapper = new QueryWrapper<>();
        /*查出父id为1，并且事实上架状态的课程*/
        subjectQueryWrapper.eq("parent_id", "0");
        subjectQueryWrapper.eq("status_id", "1");
        Page<Subject> subjectPage1 = subjectMapper.selectPage(subjectPage, subjectQueryWrapper);
        List<Subject> records = subjectPage1.getRecords();
        List<Subject> subjects = subjectMapper.selectList(null);
        for (Subject subject : records) {
            subject.setChildren(selectChildren1(subjects, subject.getId()));
        }
        return new CommonResult(200, "查询成功", subjectPage1);
    }

    /**
     *查询所有课程不同子课程的分类
     * @param subjects
     * @param pid
     * @return
     */
    public List<Subject> selectChildren1(List<Subject> subjects, String pid) {
        ArrayList<Subject> subjects1 = new ArrayList<>();
        for (Subject subject : subjects) {
            if (subject.getParentId().equals(pid) && subject.getStatusId().equals("1")) {
                subjects1.add(subject);
            }
        }

        for (Subject subject : subjects1) {
            subject.setChildren(selectChildren(subjects, subject.getId()));
        }
        return subjects1;
    }

    @Override
    public String subjectPidById(String pid) {
        Subject subject = subjectMapper.selectById(pid);
        return subject.getParentId();
    }
}
